package cn.bigcontainer.alt.leditor.controller;

import cn.bigcontainer.alt.common.exception.BigResultCode;
import cn.bigcontainer.alt.common.util.BigConstant;
import cn.bigcontainer.alt.leditor.service.LeditorService;
import cn.bigcontainer.alt.meta.dto.MAppDto;
import cn.bigcontainer.alt.meta.dto.MCrud;
import cn.bigcontainer.alt.meta.dto.MViewDto;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.annotation.Resource;
import java.util.Map;

@Controller
@RequestMapping("/meta/" + MAppDto.TYPE_LEDITOR)
@SessionAttributes(BigConstant.SESSION_USER)
public class LeditorController {

    @Resource
    private LeditorService leditorService;

    @RequestMapping
    public String loadLeditor(MAppDto app, @RequestParam Map<String, Object> req, Map<String, Object> map) {
        MCrud leditor = leditorService.loadLeditor(app.getId(), req);
        leditor.setDomprefix(app.getDomprefix());
        map.put("app", leditor);
        return "meta/" + MAppDto.TYPE_LEDITOR;
    }

    /**
     *
     * @param app
     * @param req
     * @param map
     * @return
     */
    @RequestMapping("/loadList")
    public String loadListData(MAppDto app, @RequestParam Map<String, Object> req, Map<String, Object> map) {
        MViewDto list = leditorService.loadListData(app.getId(), req);
        map.put("app", list.getApp());
        list.getApp().setDomprefix(app.getDomprefix());
        return list.getApp().getListTemplate();
    }

    /**
     *
     * @param app
     * @param req
     * @param map
     * @return
     */
    @RequestMapping("/create")
    @ResponseBody
    public BigResultCode create (MAppDto app, @RequestParam Map<String, Object> req, Map<String, Object> map) {
        MViewDto vw = leditorService.submitCreateForm(app.getId(), req);
        return BigResultCode.getSuccessCode().setContent(vw.getCallBackData());
    }

    /**
     *
     * @param app
     * @param req
     * @param map
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public BigResultCode update (MAppDto app, @RequestParam Map<String, Object> req, Map<String, Object> map) {
        MViewDto vw = leditorService.submitUpdateForm(app.getId(), req);
        return BigResultCode.getSuccessCode().setContent(vw.getCallBackData());
    }

}
